#region Copyright

// // ======================================================
// //     Copyright (c) 2011 Toulr All rights reserved.
// //    
// //     The use and distribution terms for this software are contained in the file
// //     named license.txt, which can be found in the root of this distribution.
// //     By using this software in any fashion, you are agreeing to be bound by the
// //     terms of this license.
// //    
// //     You must not remove this notice, or any other, from this software.
// // ======================================================

#endregion

#region Using Namespaces

using System;
using log4net;
using log4net.Core;

#endregion

namespace Toulr.Util.Logging.log4netIntegration
{

    #region

    #endregion

    [Serializable]
    public class Log4NetLogger : MarshalByRefObject, ILogger
    {
        private static readonly Type declaringType = typeof (Log4NetLogger);

        internal Log4NetLogger()
        {
        }

        internal Log4NetLogger(ILog log, Log4netFactory factory) : this(log.Logger, factory)
        {
        }

        public Log4NetLogger(log4net.Core.ILogger logger, Log4netFactory factory)
        {
            Logger = logger;
            Factory = factory;
        }

        protected internal log4net.Core.ILogger Logger { get; set; }

        protected internal Log4netFactory Factory { get; set; }

        #region ILogger Members

        public ILogger CreateChildLogger(String name)
        {
            return Factory.Create(Logger.Name + "." + name);
        }

        #endregion

        public override string ToString()
        {
            return Logger.ToString();
        }

        #region Debug

        public void Debug(String message)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, message, null);
            }
        }

        public void Debug(String message, Exception exception)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, message, exception);
            }
        }

        public void DebugFormat(String format, params Object[] args)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, String.Format(format, args), null);
            }
        }

        public void DebugFormat(Exception exception, String format, params Object[] args)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, String.Format(format, args), exception);
            }
        }

        public void DebugFormat(IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, String.Format(formatProvider, format, args), null);
            }
        }

        public void DebugFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, String.Format(formatProvider, format, args), exception);
            }
        }

        [Obsolete("Use DebugFormat instead")]
        public void Debug(String format, params object[] args)
        {
            if (IsDebugEnabled)
            {
                Logger.Log(declaringType, Level.Debug, String.Format(format, args), null);
            }
        }

        #endregion

        #region Info

        public void Info(String message)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, message, null);
            }
        }

        public void Info(String message, Exception exception)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, message, exception);
            }
        }

        public void InfoFormat(String format, params Object[] args)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, String.Format(format, args), null);
            }
        }

        public void InfoFormat(Exception exception, String format, params Object[] args)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, String.Format(format, args), exception);
            }
        }

        public void InfoFormat(IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, String.Format(formatProvider, format, args), null);
            }
        }

        public void InfoFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, String.Format(formatProvider, format, args), exception);
            }
        }

        [Obsolete("Use InfoFormat instead")]
        public void Info(String format, params object[] args)
        {
            if (IsInfoEnabled)
            {
                Logger.Log(declaringType, Level.Info, String.Format(format, args), null);
            }
        }

        #endregion

        #region Warn

        public void Warn(String message)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, message, null);
            }
        }

        public void Warn(String message, Exception exception)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, message, exception);
            }
        }

        public void WarnFormat(String format, params Object[] args)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, String.Format(format, args), null);
            }
        }

        public void WarnFormat(Exception exception, String format, params Object[] args)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, String.Format(format, args), exception);
            }
        }

        public void WarnFormat(IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, String.Format(formatProvider, format, args), null);
            }
        }

        public void WarnFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, String.Format(formatProvider, format, args), exception);
            }
        }

        [Obsolete("Use WarnFormat instead")]
        public void Warn(String format, params object[] args)
        {
            if (IsWarnEnabled)
            {
                Logger.Log(declaringType, Level.Warn, String.Format(format, args), null);
            }
        }

        #endregion

        #region Error

        public void Error(String message)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, message, null);
            }
        }

        public void Error(String message, Exception exception)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, message, exception);
            }
        }

        public void ErrorFormat(String format, params Object[] args)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, String.Format(format, args), null);
            }
        }

        public void ErrorFormat(Exception exception, String format, params Object[] args)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, String.Format(format, args), exception);
            }
        }

        public void ErrorFormat(IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, String.Format(formatProvider, format, args), null);
            }
        }

        public void ErrorFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, String.Format(formatProvider, format, args), exception);
            }
        }

        [Obsolete("Use ErrorFormat instead")]
        public void Error(String format, params object[] args)
        {
            if (IsErrorEnabled)
            {
                Logger.Log(declaringType, Level.Error, String.Format(format, args), null);
            }
        }

        #endregion

        #region Fatal

        public void Fatal(String message)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, message, null);
            }
        }

        public void Fatal(String message, Exception exception)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, message, exception);
            }
        }

        public void FatalFormat(String format, params Object[] args)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(format, args), null);
            }
        }

        public void FatalFormat(Exception exception, String format, params Object[] args)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(format, args), exception);
            }
        }

        public void FatalFormat(IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(formatProvider, format, args), null);
            }
        }

        public void FatalFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(formatProvider, format, args), exception);
            }
        }

        [Obsolete("Use FatalFormat instead")]
        public void Fatal(String format, params object[] args)
        {
            if (IsFatalEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(format, args), null);
            }
        }

        #endregion

        #region FatalError (obsolete)

        [Obsolete("Use FatalFormat instead")]
        public void FatalError(String format, params object[] args)
        {
            if (IsFatalErrorEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, String.Format(format, args), null);
            }
        }

        [Obsolete("Use Fatal instead")]
        public void FatalError(String message, Exception exception)
        {
            if (IsFatalErrorEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, message, exception);
            }
        }

        [Obsolete("Use Fatal instead")]
        public void FatalError(String message)
        {
            if (IsFatalErrorEnabled)
            {
                Logger.Log(declaringType, Level.Fatal, message, null);
            }
        }

        #endregion

        #region Is (...) Enabled

        public bool IsErrorEnabled
        {
            get { return Logger.IsEnabledFor(Level.Error); }
        }

        public bool IsWarnEnabled
        {
            get { return Logger.IsEnabledFor(Level.Warn); }
        }

        public bool IsDebugEnabled
        {
            get { return Logger.IsEnabledFor(Level.Debug); }
        }

        public bool IsFatalEnabled
        {
            get { return Logger.IsEnabledFor(Level.Fatal); }
        }

        [Obsolete("Use IsFatalEnabled instead")]
        public bool IsFatalErrorEnabled
        {
            get { return Logger.IsEnabledFor(Level.Fatal); }
        }

        public bool IsInfoEnabled
        {
            get { return Logger.IsEnabledFor(Level.Info); }
        }

        #endregion
    }
}